package com.sghd.candy.module.player.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.ramcache.enhance.Enhance;
import com.sghd.common.utils.math.RandomUtils;
import org.hibernate.annotations.Type;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Allen
 * @date 2018/12/07
 */
@Entity
@Table(catalog = "bsdld")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_10_MINUTE))
public class Player implements IEntity<Long> {
    /**玩家Id*/
    @Id
    private Long playerId;
    /**玩家昵称*/
    private String nickName;
    /**玩家性别*/
    private Integer sex;
    /**玩家头像Id*/
    private Integer headIconId;
    /**是否重命名*/
    private boolean renamed;
    /**玩家状态*/
    private int state;

    /**
     * 添加状态
     * */
    @Enhance("true")
    boolean addState(int state) {
        if (hasState(state)) {
            return false;
        }
        this.state |= state;
        return true;
    }

    /**
     * 是否包含state状态
     * */
    public boolean hasState(int state) {
        return (this.state & state) == state;
    }

    /**
     * 移除状态
     * */
    @Enhance("true")
    boolean removeState(int state) {
        if (!hasState(state)) {
            return false;
        }
        this.state ^= state;
        return true;
    }

    @Enhance
    public void modifyHeadIcon(int headIconId) {
        setHeadIconId(headIconId);
    }

    @Enhance("true")
    public boolean rename(String nickName) {
        if (!renamed) {
            setNickName(nickName);
            setRenamed(true);
            return true;
        } else {
            return false;
        }
    }

    public Long getPlayerId() {
        return playerId;
    }

    void setPlayerId(Long playerId) {
        this.playerId = playerId;
    }

    public String getNickName() {
        return nickName;
    }

    void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Integer getSex() {
        return sex;
    }

    void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getHeadIconId() {
        return headIconId;
    }

    void setHeadIconId(Integer headIconId) {
        this.headIconId = headIconId;
    }

    public boolean isRenamed() {
        return renamed;
    }

    public void setRenamed(boolean renamed) {
        this.renamed = renamed;
    }

    public int getState() {
        return state;
    }

    void setState(int state) {
        this.state = state;
    }

    public static Player valueOf(long playerId) {
        Player player = new Player();
        player.setPlayerId(playerId);
        player.setHeadIconId(RandomUtils.nextInt(100));
        player.setSex(0);
        player.setNickName("斗者" + playerId);
        player.setRenamed(false);
        return player;
    }

    @Override
    public Long getId() {
        return this.playerId;
    }

}
